package com.gj.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gj.reggie.dto.DishDto;
import com.gj.reggie.entity.Dish;
import com.gj.reggie.entity.DishFlavor;
import com.gj.reggie.mapper.DishFlavorMapper;
import com.gj.reggie.mapper.DishMapper;
import com.gj.reggie.service.DishFlavorService;
import com.gj.reggie.service.DishService;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 郭嘉
 * @date 2022/12/29 - 16:11
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 新增菜品信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存基本菜品到菜品表
        this.save(dishDto);
        Long dishId=dishDto.getId();
        List<DishFlavor> dishFlavorList=dishDto.getFlavors();
        dishFlavorList=dishFlavorList.stream().map((item)->{
           item.setDishId(dishId);
           return item;
        }).collect(Collectors.toList());
        //保存菜品口味添加到菜品口味表
        dishFlavorService.saveBatch(dishFlavorList);
    }

    @Override
    public DishDto getDishWithFlavorById(Long id) {
        //查询菜品基本信息
        Dish dish=this.getById(id);
        DishDto dishDto=new DishDto();
        //对象拷贝
        BeanUtils.copyProperties(dish,dishDto);
        //查询菜品的口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavorList=dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(dishFlavorList);

        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //先清理当前菜品对应口味数据
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //添加当前提交过来的口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public void deleteDishWithFlavor(Long[] ids) {
        if(ids.length==1){
            this.removeById(ids);
            LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,ids);
            dishFlavorService.remove(queryWrapper);
        }else{
            List<Long> list=new ArrayList<>();
            for(int i=0;i<ids.length;i++){
                list.add(ids[i]);
            }
            this.removeByIds(list);
            dishFlavorMapper.deleteMoreDishFlavor(ids);
        }
    }

    @Override
    public void updateDishStatus(Long[] ids, Integer status) {
        if(ids.length==1){
            Dish dish=this.getById(ids);
            dish.setStatus(status);
            this.updateById(dish);
        }else{
            dishMapper.updateMoreDishStatus(status,ids);
        }
    }
}
